package hafnium.bukkit.util.redstone;

public enum Current {
	RISING(true, true), // Current going from off to on.
	FALLING(false, true), // Current going from on to off.
	ON(true, false), // Current is on and steady.
	OFF(false, false), // Current is off and steady.
	DISCONN(false, false), // There is no block at the location.
	UNLOADED(false, false); // The location is not loaded.

	private final boolean isPowered;
	private final boolean isChanging;

	private Current(boolean isPowered, boolean isChanging) {
		this.isPowered = isPowered;
		this.isChanging = isChanging;
	}

	/**
	 * Returns true iff the Current represents a presence of redstone power.
	 * 
	 * @return true iff the Current represents a presence of redstone power.
	 */
	public boolean asBoolean() {
		return this.isPowered;
	}

	/**
	 * Returns true iff the Current represents a changing level.
	 * 
	 * @return true iff the Current represents a changing level.
	 */
	public boolean isChanging() {
		return this.isChanging;
	}

	/**
	 * Gets the conclusion of the current, aka what the current is or is
	 * changing into.
	 * 
	 * @return
	 */
	public Current getConclusion() {
		if (!isChanging())
			return this;

		switch (this) {
		case RISING:
			return ON;
		case FALLING:
			return OFF;
		}

		return null;
	}

	public static Current interpolate(Current from, Current to) {
		if (from.asBoolean() == false && to.asBoolean() == true)
			return Current.RISING;

		if (from.asBoolean() == true && to == Current.OFF)
			return Current.FALLING;

		return to;
	}
}
